package space.edge.d.demo.qrcode.privatecode;

import space.edge.d.demo.qrcode.privatecode.encoder.Code;
import space.edge.d.demo.qrcode.common.EncodeHintType;
import space.edge.d.demo.qrcode.privatecode.encoder.Encoder;
import space.edge.d.demo.qrcode.common.ErrorCorrectionLevel;
import space.edge.d.demo.qrcode.exception.WriterException;

import java.util.Map;

/**
 * Created by gsl on 2017/9/26.
 */

public class CodeWriter {

    public Code encode(String contents, Map<EncodeHintType, ?> hints) throws WriterException {
        if (contents == null || contents.isEmpty()) {
            throw new IllegalArgumentException("Found empty contents");
        }

        return Encoder.encode(contents, getLevelFromHint(hints), hints);
    }

    public Code encode(byte[] contents, Map<EncodeHintType, ?> hints) throws WriterException {
        if (contents == null || contents.length == 0)
            throw new IllegalArgumentException("Found empty contents");
        return Encoder.encode(contents, getLevelFromHint(hints), hints);
    }

    private ErrorCorrectionLevel getLevelFromHint(Map<EncodeHintType, ?> hints) {
        ErrorCorrectionLevel level = ErrorCorrectionLevel.L;
        if (hints != null) {
            if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
                level = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
            }
        }
        return level;
    }
}
